//
//  MyStatus.swift
//  weibo08
//
//  Created by 陈杰明 on 16/3/5.
//  Copyright © 2016年 JM. All rights reserved.
//

import UIKit

class MyStatus: NSObject {

    // 微博创建时间
    var created_at: String?
    // 微博ID
    var id : Int = 0
    // 微博内容
    var text: String?
    // 来源
    var source: String?
    //图片
    var pic_urls: [[String : AnyObject]]? {
        didSet{
            let count = pic_urls?.count ?? 0
            
            if count == 0 {
                return
            }
            //创建数组保存图片地址
            storePictureURL = [NSURL]()
            
            for dict in pic_urls! {
            
                let urlStr = dict["thumbnail_pic"] as! String
                
                let url = NSURL(string: urlStr)
                
                storePictureURL?.append(url!)
            }
        }
    }
    
    //微博配图，存放图片的url
    var storePictureURL: [NSURL]?
    // 转发数
    var reposts_count: Int = 0
    // 评论数
    var comments_count: Int = 0
    // 点赞数
    var attitudes_count: Int = 0
    //用户
    var user: MyUser?
    //转发微博
    var retweeted_status: MyStatus?
    //字典转模型
    init(dict: [String : AnyObject]) {
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forKey key: String) {
        //判断key == user ,字典转模型
        if key == "user" {
            if let dict = value as? [String : AnyObject] {
            
                user = MyUser(dict: dict)

            }
            //return ,不走父类将字典传给user属性
            return
        }else if key == "retweeted_status" {
            if let dict = value as? [String: AnyObject] {
            
                retweeted_status = MyStatus(dict: dict)
            }
            //return ,不走父类将字典传给retweeted_status属性
            return
        }
        super.setValue(value, forKey: key)
    }
    
    //忽略找不到的key
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    //打印
    override var description: String {
    let keys = ["created_at", "id", "text", "source", "pic_urls", "reposts_count", "comments_count", "attitudes_count", "user"]

       return "\n\t微博模型:\(dictionaryWithValuesForKeys(keys).description)"
    
    }
}
